<?php
	/**
	 * @package AmpleHarvest
	 */
	require_once 'TableHeader.php';

	/**
	 * @brief This class Describes the Query_History Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 07 NOV 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cTABLE_GroupOptions extends cTable
	{

		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = null;
		public $m_int_COL_GroupID = null;
		public $m_int_COL_Key = null;
		public $m_int_COL_Value = null;
		/**
		 * @}
		 */

		/**
		 * @{
		 * @name Permissions
		 */
		const C_STR_PERM_TAGGING_ADD = "tagging_add";
		const C_STR_PERM_TAGGING_REMOVE = "tagging_remove";
		const C_STR_PERM_TAGGING_TYPE = "tagging_type";
		/**
		 * @}
		 */

		const C_INT_PERM_TAGGING_NONE = 0;
		const C_INT_PERM_TAGGING_ALL = 1;
		const C_INT_PERM_TAGGING_MEMBERS = 2;
		const C_INT_PERM_TAGGING_ADMIN = 3;

		const C_INT_PERM_TAGGING_ADHOCK = 0;
		const C_INT_PERM_TAGGING_LIMITED = 1;

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "group_options";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of the referance.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_Group_ID = new cColVarChar("group_id");
			$obj_COL_Group_ID->set_Length(36);
			$obj_COL_Group_ID->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Group_ID->set_Comment("This column holds the ID of the Group the option is for.");
			$obj_COL_Group_ID->set_Editable(false);
			$obj_COL_Group_ID->set_Label("Group ID");

			$obj_COL_Key = new cColVarChar("key");
			$obj_COL_Key->set_Length(200);
			$obj_COL_Key->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Key->set_Comment("This column holds the Key of the options you are storeing.");
			$obj_COL_Key->set_Label("Key");

			$obj_COL_Value = new cColBlob("value");
			$obj_COL_Value->set_Comment("The Value to stored.");
			$obj_COL_Value->set_Label("Value");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//$this->m_obj_Connection->activate_Logging();
			//$this->m_obj_Connection->activate_LogDebug();
			//$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_GroupID = $this->add_Column($obj_COL_Group_ID);
			$this->m_int_COL_Key = $this->add_Column($obj_COL_Key);
			$this->m_int_COL_Value = $this->add_Column($obj_COL_Value);
		}

		/**
		 * This function is used to insert Data into the database
		 */
		public function insert_Data2DB()
		{
			$this->set_id($this->create_GUID());

			return parent::insert_Data2DB();
		}

		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			if ($this->validate_ID($this->get_id()))
			{
				return $this->update_Data2DB();
			}
			else
			{
				return $this->insert_Data2DB();
			}
		}

		public function insert_DefaultData()
		{
			return true;
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the Page to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		public function set_KeyValue($str_GroupID, $str_Key, $str_Value = "")
		{
			$this->load_DataFromGroupKey($str_GroupID, $str_Key);
			
			$this->set_group_id($str_GroupID);
			$this->set_key($str_Key);
			$this->set_value($str_Value);
			
			$this->save_Data2DB();
		}

		public function load_DataFromGroupKey($str_GroupID, $str_Key)
		{
			$this->clear();
			if (!isset($str_GroupID) || empty($str_GroupID))
			{
				return false;
			}
			if (!isset($str_Key) || empty($str_Key))
			{
				return false;
			}
			$this->clear_Query();
			$this->add_ColumnQuery($this->m_int_COL_GroupID, $str_GroupID);
			$this->add_ColumnQuery($this->m_int_COL_Key, $str_Key);

			if ($this->do_Search() > 0)
			{
				$obj_RS = $this->get_ResultSet();
				if ($obj_RS[0])
				{
					$this->load_DataFromArray($obj_RS[0]);
					return true;
				}
			}
			return false;
		}

		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the id to check (int)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if ($this->validate_ID($str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}

	}
?>
